﻿@model NCLCustomerManagement.UI.Models.User.UserProfileModel
@{
    Layout = "";
}
<div id="usermsg" class="alert alert-danger" style="display: none;">

    <div class="msg-content">
        <strong>Warning!</strong> Best check yo self, you're not looking too good.
    </div>

</div>
@Html.Partial("_UserPartial", Model as object)

<div class="btn-toolbar">
    <div class="bt-n-group" style="float: right;">
        <button id="btnSave" class="btn btn-primary" onclick="save();"><span class="glyphicon glyphicon-save" title="Lưu">Lưu</span></button>
        <button id="btnCancel" class="btn btn-default" onclick="cancel();"><span class="glyphicon glyphicon-log-out" title="Hủy">Hủy</span></button>
    </div>
</div>

<script>
    function validateUser(model, validatedFunc) {
        var err = [];
        if (model.username == '') {
            err.push("Bạn chưa nhập tên đăng nhập");
        }
        //if (model.password == '') {
        //    err.push("Bạn chưa nhập mật khẩu");
        //}
        if (model.email == '') {
            err.push("Bạn chưa nhập email");
        }
        else {
            if (!validateEmail(model.email)) {
                err.push("Địa chỉ email không hợp lệ!");
            }
        }
        checkUserExists(model.username, function (data) {
            if (data.Exist)
                err.push("User đã tồn tại, bạn hãy chọn tên khác");
            if (err.length > 0) {
                showMessage("#usermsg", err.join("<br/>"), true);
            }
            else {
                validatedFunc();
            }
        });


        function checkUserExists(username, callback) {
            $.ajax({
                url: "/User/CheckUserExists?username=" + username,
                data: {},
                contentType: 'application/json',
                dataType: "json",
                type: "get",
                success: function (data) {
                    callback(data);
                },
                error: function (data) {
                    window.location.href = "/User/Login";
                }
            });
        }
    }

    function getUserModel() {
        return {
            username: $("#Username").val(),
            isAdmin: $("#IsAdmin").attr("checked") == "checked",
            email: $("#Email").val()
        };
    }

    function save() {
        var model = getUserModel();
       
        validateUser(model, function () {
            $.blockUI();
            var d = JSON.stringify(model);
            $.ajax({
                url: "/User/AddUser",
                data: { userJson: d },
                contentType: 'application/json',
                dataType: "json",
                type: "get",
                success: function (data) {
                    $("#list").jqGrid('setGridParam', { datatype: 'json' });
                    $('#list').trigger('reloadGrid');
                    showMessage("#alertbox", data.Message, !data.Success);
                    cancel();
                    $.unblockUI();
                },
                error: function (data) {
                    //window.location.href = "/User/Login";
                    $.unblockUI();
                }
            });
        });
    }

    function cancel() {
        $('#editWindow').modal('hide');
    }


</script>
